/*
 * Copyright (C) 2014 TinhVan Outsourcing.
 * 
 * BaseDialog.java
 * 
 * Created on: 04-01-2014 - 11:18:59 Author: VanNT
 */
package vn.anhqn.englishsentensetranslator.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 * @author VanNT 04-01-2014
 */
public abstract class BaseDialog extends Dialog {
    private static final String LOG_TAG = BaseDialog.class.getSimpleName();
    private boolean mIsDialogFullScreen;

    /**
     * @param context
     * @param cancelable
     * @param cancelListener
     */
    protected BaseDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        setActivity(context);
    }

    /**
     * @param context
     * @param theme
     */
    public BaseDialog(Context context, int theme) {
        super(context, theme);
        setActivity(context);
    }

    /**
     * @param context
     */
    public BaseDialog(Context context) {
        super(context);
        setActivity(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mIsDialogFullScreen = isDialogFullScreen();
        configDialog();
        init();
    }

    protected void configDialog() {
        super.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    private void init() {
        final View pContent = getLayoutInflater().inflate(getLayoutResId(), null, false);
        super.setContentView(pContent);
        super.getWindow().setAttributes(setDialogLayoutParams(getContextDialog(), this, mIsDialogFullScreen));
        this.onCreateContent(pContent);
        setCanceledOnTouchOutside(false);
    }

    public static WindowManager.LayoutParams setDialogLayoutParams(Context pContext, Dialog dialog,
            final boolean pIsDialogFullScreen) {
        try {
            final DisplayMetrics displayMetrics = Utils.getDeviceSize(pContext);
            WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
            layoutParams.copyFrom(dialog.getWindow().getAttributes());
            layoutParams.gravity = Gravity.CENTER;
            layoutParams.flags &= ~WindowManager.LayoutParams.FLAG_BLUR_BEHIND;
            layoutParams.windowAnimations = android.R.style.Animation_Dialog;
            int width = displayMetrics.widthPixels;
            //int height = displayMetrics.heightPixels;
            width = (int) (width - (width * 0.12));
            /*if (!pIsDialogFullScreen) {
                width = (int) (width - (width * 0.07));
                height = (int) (height - (height * 0.07));
            }*/
            layoutParams.width = width;
            //layoutParams.height = height;
            layoutParams.dimAmount = 0.85f;
            /* WindowManager.LayoutParams.WRAP_CONTENT */;
            return layoutParams;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    protected boolean isDialogFullScreen() {
        return false;
    }

    protected abstract int getLayoutResId();

    protected abstract void onCreateContent(View pContentView);

    // protected

    protected String getStringRes(final int pResId) {
        return getContext().getResources().getString(pResId);
    }

    @SuppressWarnings("unchecked")
    public <T extends View> T findViewByIdDialog(int id) {
        return (T) this.findViewById(id);
    }

    public static <T extends View> T findViewById(final View pParentView, final int pIdView) {
        return Utils.findViewById(pParentView, pIdView);
    }

    private void setActivity(Context pContext) {
        // chances of context not being an activity is very low, but better to check.
        final Activity owner = (pContext instanceof Activity) ? (Activity) pContext : null;
        if (owner != null) {
            setOwnerActivity(owner);
        }
    }

    
    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        // getOwnerActivity() should be defined here if called via showDialog(), so do the related
        // init here
        Activity owner = getOwnerActivity();
        if (owner != null) {
            // owner activity defined here
        }
    }

    /**
     * check and return OwnerActivity if can else return Context
     * 
     * @return Context of Activity
     */
    public Context getContextDialog() {
        return getOwnerActivity() == null ? getContext() : getOwnerActivity();
    }

    @Override
    public void show() {
        try {
            super.show();
        } catch (WindowManager.BadTokenException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void dismiss() {
        try {
            super.dismiss();
        } catch (java.lang.IllegalArgumentException e) {
            e.printStackTrace();
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }
}
